import axios from 'axios';
// import store from '@/store/store.js'
// import * as types from './store/types'
import router from './router/index'
// import { resolve } from 'dns';

// axios 配置

axios.defaults.timeout = 10000;
// axios.defaults.baseURL = 'http://localhost:8000/appApi/v1';
axios.defaults.baseURL = 'appApi/v1';
axios.defaults.withCredentials = true; 

// 添加请求拦截器
axios.interceptors.request.use(function(config) {
    // 在发送请求之前做些什么
    var token = window.localStorage.getItem('token');
    if (token) {
    	config.headers.Authorization = token;
    }
    config.data = JSON.stringify(config.data);
    console.log(config.data);
    
    config.headers = {
        'Content-Type': 'application/json',
        'X-Requested-With': 'XMLHttpRequest'
    }
    return config;
}, function(error) {
    // 对请求错误做些什么
    console.log(222);
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.errCode == 2) {
            router.push({
                path: "/login",
                querry: { redirect: router.currentRoute.fullPath }
            })
        }
        return response;
    },
    error => {
        if (error.response) {

            switch (error.response.status) {
                case 401:
                    // 返回 401 清除token信息并跳转到登录页面
                    // store.commit(types.LOGOUT);
                    router.replace({
                        path: 'login',
                        query: { redirect: router.currentRoute.fullPath }
                    })
            }
        }
        console.log(error);
        return Promise.reject(error);
    }
);


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 * 
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(response => {
                console.log(response);
                resolve(response);
            })
            .catch(err => {
                reject(err)
            })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 * 
 */

export function post(url, data = {}) {
    console.log(data);
    
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                console.log(response);
                
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}
/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}
export default axios;